from SiderTools import *
import requests

music_types = [
    {
        'name': "华语",
        'subCateId': 2368
    },
    {
        'name': "欧美",
        'subCateId': 2369
    }
]

# 1、加密sign,发送请求，获取数据
def get_json(page):
    timestamp = publicObj.getTimeStamp()
    args = {
        "sign": "",
        "appid": publicObj.appid,  # 固定
        "subCateId": music_types[1]['subCateId'],  # 类别，获取欧美类别的歌曲
        "pageNo": page,  # 第几页
        "timestamp": timestamp,  # 时间
        'pageSize': 50  # 每页的数据
    }
    signParam = "appid={0}&pageNo={1}&pageSize={2}&subCateId={3}&timestamp={4}{5}".format(
        args['appid'], args['pageNo'], args['pageSize'], args['subCateId'], timestamp, publicObj.secret)
    print("加密之前signParm", signParam)
    # 加密
    args['sign'] = publicObj.mdEncode(signParam)
    print(f"最终的参数{args}")

    result = requests.get(url=publicObj.song_sheet_type, params=args)
    json = result.json()
    print(f"获取到的数据{json}")
    return json

# 解析数据并写入 CSV
def save_data_csv(page):
    json_result = get_json(page)
    music_type_list = json_result['data']['result']

    for music_type in music_type_list:
        # print(music_type)
        id = music_type['id']
        img = music_type['pic']
        title = music_type['title']
        desc = music_type['desc']
        taglist = str(music_type['tagList'])
        # result = str(data["taglist"])
        type = music_type['resourceType']
        musicNum = music_type['trackCount']
        createTime = music_type['addDate']

        row_data = [id,img,title,desc,taglist,type,musicNum,createTime]
        publicObj.init_song_sheet_data(row_data)


# 4、存储数据
if __name__ == '__main__':
    publicObj = PublicSiderTools()
    # publicObj.create_song_sheet_type()
    save_data_csv(1)